/// Business logic class defined by OpenAPI in HCC.Interface.Setting.spec<br/>
/// Updated May 15, 2023 04:36:57
Class HCC.Interface.Setting.impl Extends %REST.Impl [ ProcedureBlock ]
{

/// If ExposeServerExceptions is true, then details of internal errors will be exposed.
Parameter ExposeServerExceptions = 1;

/// 获取系统设置参数
ClassMethod GetSystemSetting() As %Stream.Object
{
    Set tOutput = ##class(%DynamicObject).%New()
    Set tOutput."跳过值集检查" = $BIT(^WS.Setting.Validation,1)
    Set tOutput."跳过数据元必要性约束检查" = $BIT(^WS.Setting.Validation,2)
    Set tOutput."跳过章节必要性检查" = $BIT(^WS.Setting.Validation,3)
    Set tOutput.HostFacility=##class(%DynamicObject).%New()
    Set tOutput.HostFacility.Id = $LISTGET($GET(^HCC("HostFacility")),1)
    Set tOutput.HostFacility.Name=$LISTGET($GET(^HCC("HostFacility")),2)
    Set tOutput."XSLFilePath"=$GET(^HCC("XSLFilePath"))
    Set tOutput."DocGenTask"=$GET(^HCC("DocGenTask"))
    Set tOutput."Debug模式"=$GET(^HCC("Debug"))
    Return tOutput
}

/// 开启共享文档生成任务
ClassMethod StartSQLDocTask() As %Stream.Object
{
    Job ##class(HCC.Util).CreateDocTask()
    Return 1
}

/// 停止共享文档生成任务
ClassMethod StopSQLDocTask() As %Stream.Object
{
   
    Set tsc=  ##class(HCC.Util).StopCreateDocTask()
    If $$$ISERR(tsc) {
        Return ##class(HCC.Utils.ResponseHelper).GetAPIErrorResponse("停止共享文档生成任务失败",tsc.AsStatus(),500)
    }
    Return tsc
}

/// 跳过数据元约束检查
ClassMethod SkipDEValidation() As %Stream.Object
{
    Set tSC = ##class(HCC.Setting).SkipDEValidation()
    If $$$ISERR(tSC) {
        Return ##class(HCC.Utils.ResponseHelper).GetAPIErrorResponse("跳过数据元约束检查失败",tSC.AsStatus(),500)
    }
    Return $$$ISOK(tSC)
}

/// 开启数据元约束检查
ClassMethod EnforceDEValidation() As %Stream.Object
{
    Set tSC = ##class(HCC.Setting).EnforceDEValidation()
    If $$$ISERR(tSC) {
        Return ##class(HCC.Utils.ResponseHelper).GetAPIErrorResponse("开启数据元约束检查失败",tSC.AsStatus(),500)
    }
    Return $$$ISOK(tSC)
}

/// 跳过数据元必要性约束检查
ClassMethod SkipDECardValid() As %Stream.Object
{
   Set tSC = ##class(HCC.Setting).SkipDECardValid()
   If $$$ISERR(tSC) {
        Return ##class(HCC.Utils.ResponseHelper).GetAPIErrorResponse("跳过数据元必要性约束检查失败",tSC.AsStatus(),500)
    }
    Return $$$ISOK(tSC)
}

/// 开启数据元必要性约束检查
ClassMethod EnforceDECardValid() As %Stream.Object
{
      Set tSC = ##class(HCC.Setting).EnforceDECardValid()
      If $$$ISERR(tSC) {
        Return ##class(HCC.Utils.ResponseHelper).GetAPIErrorResponse("开启数据元必要性约束检查失败",tSC.AsStatus(),500)}
    Return $$$ISOK(tSC)
}

/// 设置xsl文件路径<br/>
/// The method arguments hold values for:<br/>
///     path, xsl文件路径<br/>
ClassMethod SetXSLPath(path As %Stream.Object) As %Stream.Object
{
    #dim tFilePath As %DynamicObject
    Try {
        Set tFilePath=##class(%DynamicObject).%FromJSON(path)
    Set tSC = ##class(HCC.Setting).SetXSLPath(tFilePath.Path)
    }
    Catch ex {
        Return ##class(HCC.Utils.ResponseHelper).GetAPIErrorResponse("请提供正确的xsl文件路径",ex.AsStatus(),400)
    }
    
    Return $$$ISOK(tSC)
}

/// 设置debug文件夹路径<br/>
/// The method arguments hold values for:<br/>
///     path, debug文件夹路径<br/>
ClassMethod SetDebugFilePath(path As %Stream.Object) As %Stream.Object
{
       #dim tFilePath As %DynamicObject
    Try {
        Set tFilePath=##class(%DynamicObject).%FromJSON(path)
    Set tSC = ##class(HCC.Setting).SetDebugFilePath(tFilePath.Path)
    }
    Catch ex {
        Return ##class(HCC.Utils.ResponseHelper).GetAPIErrorResponse("请提供正确的debug 文件夹路径",ex.AsStatus(),400)
    }
    
    Return $$$ISOK(tSC)
}

/// 开启debug模式
ClassMethod EnableDebugMode() As %Stream.Object
{
    Set tSC = ##class(HCC.Setting).EnableDebugMode()
    If $$$ISERR(tSC) {
        Return ##class(HCC.Utils.ResponseHelper).GetAPIErrorResponse("开启debug模式失败",tSC.AsStatus(),500)
    }
    Return $$$ISOK(tSC)
}

/// 关闭debug模式
ClassMethod DisableDebugMode() As %Stream.Object
{
    Set tSC = ##class(HCC.Setting).DisableDebugMode()
    If $$$ISERR(tSC) {
        Return ##class(HCC.Utils.ResponseHelper).GetAPIErrorResponse("关闭debug模式失败",tSC.AsStatus(),500)
    }
    Return $$$ISOK(tSC)
}

/// 获取匿名化设置
ClassMethod GetAnonySetting() As %Stream.Object
{
   Try {
     Set anonySetting = ##class(HCC.Utils.Anonymization).%New()
    Set resp ={}
    Set resp.Enable = anonySetting.Enable
    Set resp.patientAddrs = anonySetting.patientAddrs
    Set resp.patientIds = anonySetting.patientIds
    Set resp.patientName = anonySetting.patientName
    Set resp.patientTelcom = anonySetting.patientTelcom
   }
   Catch ex {
    Return ##class(HCC.Utils.ResponseHelper).GetAPIErrorResponse("获取匿名化设置失败",ex.AsStatus(),500)
   }
    
    Return resp
}

/// 更新匿名化设置<br/>
/// The method arguments hold values for:<br/>
///     path, debug文件夹路径<br/>
ClassMethod SetAnonySetting(path As %Stream.Object) As %Stream.Object
{
    Try {
          #dim tFilePath As %DynamicObject
    Set anonySetting={}.%FromJSON(%request.Content)

    Set anony = ##class(HCC.Utils.Anonymization).%New()
    Set anony.Enable = anonySetting.Enable
    Set anony.patientAddrs = anonySetting.patientAddrs
    Set anony.patientIds = anonySetting.patientIds
    Set anony.patientName = anonySetting.patientName
    Set anony.patientTelcom = anonySetting.patientTelcom
    Set resp = {"Result": "更新设置成功！"}
    Set resp.settings = anonySetting

    Write resp
    }
    Catch ex {
        Return ##class(HCC.Utils.ResponseHelper).GetAPIErrorResponse("更新匿名化设置失败",ex.AsStatus(),500)
    }
    Return resp
}

// ClassMethod SetHCCProduction(productionName As %Stream.Object) As %Stream.Object

// {

//     #dim tFilePath As %DynamicObject

//     Try {

//         Set tFilePath=##class(%DynamicObject).%FromJSON(productionName)

//         if (##class(%Dictionary.CompiledClass).%ExistsId(tFilePath.Name)) {

//         Set ^HCC("HCCProductionName")=tFilePath.Name}

//         else{

//             Return ##class(HCC.Utils.ResponseHelper).GetAPIErrorResponse("Production 不存在",$$$OK,404)

//         }

//     }

//     Catch ex {

//         Return ##class(HCC.Utils.ResponseHelper).GetAPIErrorResponse("请提供正确的production 名字，保证此production已经存在",ex.AsStatus(),400)

//     }

//     Return $$$OK

// }

/// 设置HCC 使用的production 名字<br/>
/// The method arguments hold values for:<br/>
///     productionName, production 名字<br/>

}
